package com.kla.power.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.kla.power.entity.UserEntity;
import com.kla.power.mapper.UserMapper;
import com.kla.power.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kla.power.util.CommonUtil;
import com.kla.power.util.StateEnum;
import com.kla.power.util.StateException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author WuZH
 * @since 2024-06-02
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements IUserService {


    @Resource
    private UserMapper userMapper;

    @Override
    public List<UserEntity> queryList ( UserEntity userEntity ) {
        if ( userEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        userEntity.setPage ( ( userEntity.getPage () - 1 ) * userEntity.getLimit () );
        List<UserEntity> userEntities = userMapper.queryList ( userEntity );
        return userEntities;
    }

    @Override
    public Integer queryCount ( UserEntity userEntity ) {
        if ( userEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        Integer integer = userMapper.queryCount ( userEntity );
        return integer;
    }

    @Override
    public UserEntity saveSingle ( UserEntity userEntity ) {
        if ( userEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        if ( userEntity.getId () == null ) {
            throw new StateException ( StateEnum.PRIMARY_NULL_EXCE );
        }
        userEntity.setPassword ( CommonUtil.MD5Util ( "123456" ) ); // 默认密码，可改，设置密码盐
        userEntity.setCreateTime ( new Date () );
        int insert = baseMapper.insert ( userEntity );
        if ( insert == 0 ) {
            throw new StateException ( StateEnum.INSERT_EXCE );
        }
        return userEntity;
    }

    @Override
    public UserEntity querySingle ( UserEntity userEntity ) {
        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<> ();
        if ( StringUtils.isNotBlank ( userEntity.getId () ) ) {
            queryWrapper.eq ( "id", userEntity.getId () );
        }
        if ( StringUtils.isNotBlank ( userEntity.getTelPhone () ) ) {
            queryWrapper.eq ( "telPhone", userEntity.getTelPhone () );
        }
        if ( StringUtils.isNotBlank ( userEntity.getPassword () ) ) {
            queryWrapper.eq ( "password", CommonUtil.MD5Util ( userEntity.getPassword () ) );
        }
        UserEntity user = baseMapper.selectOne ( queryWrapper );
        return user;
    }

    @Override
    public UserEntity updateSingle ( UserEntity userEntity ) {
        UpdateWrapper<UserEntity> updateWrapper = new UpdateWrapper<> ();
        if ( StringUtils.isNotBlank ( userEntity.getName () ) ) {
            updateWrapper.set ( "name", userEntity.getName () );
        }
        if ( StringUtils.isNotBlank ( userEntity.getPassword () ) ) {
            updateWrapper.set ( "password", userEntity.getPassword () );
        }
        if ( StringUtils.isNotBlank ( userEntity.getMerchantId () ) ) {
            updateWrapper.set ( "merchantId", userEntity.getMerchantId () );
        }
        if ( StringUtils.isNotBlank ( userEntity.getRoleId () ) ) {
            updateWrapper.set ( "roleId", userEntity.getRoleId () );
        }
        if ( StringUtils.isNotBlank ( userEntity.getIdCard () ) ) {
            updateWrapper.set ( "idCard", userEntity.getIdCard () );
        }
        if ( userEntity.getAge () != null ) {
            updateWrapper.set ( "age", userEntity.getAge () );
        }
        if ( StringUtils.isNotBlank ( userEntity.getEmail () ) ) {
            updateWrapper.set ( "email", userEntity.getEmail () );
        }
        if ( StringUtils.isNotBlank ( userEntity.getHeadImgUrl () ) ) {
            updateWrapper.set ( "headImgUrl", userEntity.getHeadImgUrl () );
        }
        if ( userEntity.getLastLoginTime () != null ) {
            updateWrapper.set ( "lastLoginTime", userEntity.getLastLoginTime () );
        }
        if ( userEntity.getIsUsed () != null ) {
            updateWrapper.set ( "isUsed", userEntity.getIsUsed () );
        }
        userEntity.setChangeTime ( new Date () );
        updateWrapper.eq ( "id", userEntity.getId () );
        int update = baseMapper.update ( null, updateWrapper );
        if ( update == 0 ) {
            throw new StateException ( StateEnum.UPDATE_EXCE );
        }
        return userEntity;
    }
}
